package com.treasure.mall.zbqmallapi.config;

import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.base.ResultCodeEnum;
import com.treasure.mall.zbq.common.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallapi.config
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/6/14 15:01
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * Copyright 2019-2022 com.treasure.mall.zbqmallapi.config Inc. All rights reserved.
 */
@Controller
@ControllerAdvice
@Slf4j
public class ApiZbExceptionHandler {
    /**
     * 所有异常统一处理
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ApiResult runtimeExceptionHandler(Exception ex) {
        if (ex instanceof ApiException) {
            ApiException apiException = (ApiException) ex;
            return apiException.getApiResult();
        }
        return ApiResult.errorWith(ResultCodeEnum.EXCEPTION, ex.getMessage());
    }
}
